package com.gscitysfy.cus.drain.modules.reserveplan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gscitysfy.cus.drain.modules.reserveplan.dto.DrainReservePlanMnCarDto;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanControlInfo;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanMnCar;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanMnCarMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.service.IDrainReservePlanMnCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.modules.resource.entity.ResourceCar;
import com.gscitysfy.cus.drain.modules.resource.mapper.ResourceCarMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shenglin
 * @since 2022-11-15 14:28:05
 */
@Service
public class DrainReservePlanMnCarServiceImpl extends ServiceImpl<DrainReservePlanMnCarMapper, DrainReservePlanMnCar> implements IDrainReservePlanMnCarService {

    @Autowired
    private DrainReservePlanMnCarMapper drainReservePlanMnCarMapper;

    @Autowired
    private ResourceCarMapper resourceCarMapper;

    @Override
    public void deletByInfoId(List<String> infoIds) {
        LambdaQueryWrapper<DrainReservePlanMnCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanMnCar::getInfoId,infoIds);
        List<DrainReservePlanMnCar> reservePlanMnCars = drainReservePlanMnCarMapper.selectList(queryWrapper);
        for (DrainReservePlanMnCar reservePlanMnCar : reservePlanMnCars) {
            reservePlanMnCar.setCmUsable(true);
        }
        this.updateBatchById(reservePlanMnCars);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int saveBathReservePlanMnCarInfo(List<DrainReservePlanMnCar> drainReservePlanMnCars) {
        if(!drainReservePlanMnCars.isEmpty()){
            //删除原有的应急车辆信息
            LambdaQueryWrapper<DrainReservePlanMnCar> queryWrapper = new LambdaQueryWrapper<>();
            List<DrainReservePlanMnCar> old = drainReservePlanMnCarMapper.selectList(queryWrapper);
            for (DrainReservePlanMnCar reservePlanMnCar : old) {
                reservePlanMnCar.setCmUsable(true);
            }
            this.updateBatchById(old);
            int i=0;
            for (DrainReservePlanMnCar reservePlanMnCar : drainReservePlanMnCars) {
                //预案应急车辆的预案id不能为空
                if(reservePlanMnCar!=null&& reservePlanMnCar.getInfoId()!=null){
                    i+=drainReservePlanMnCarMapper.insert(reservePlanMnCar);
                }
            }
            return i;
        }
        return 0;
    }

    @Override
    public List<DrainReservePlanMnCarDto> getReservePlanMnCarInfoList(String infoId, String level) {
        LambdaQueryWrapper<DrainReservePlanMnCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanMnCar::getInfoId,infoId);
        queryWrapper.eq(DrainReservePlanMnCar::getCmUsable,false);
        if(StringUtils.isNotBlank(level)){
            queryWrapper.eq(DrainReservePlanMnCar::getLevel,level);
        }
        //查询预案关联的应急车辆
        List<DrainReservePlanMnCar> reservePlanMnCars = drainReservePlanMnCarMapper.selectList(queryWrapper);
        List<DrainReservePlanMnCarDto> result =new ArrayList<>();
        for (DrainReservePlanMnCar reservePlanMnCar : reservePlanMnCars) {
            //通过应急车辆id查询车辆详细信息
            ResourceCar resourceCar = resourceCarMapper.selectById(reservePlanMnCar.getMnCarId());
            if(resourceCar!=null){
                DrainReservePlanMnCarDto dto=new DrainReservePlanMnCarDto();
                //复制车辆信息
                BeanUtils.copyProperties(resourceCar,dto);
                BeanUtils.copyProperties(reservePlanMnCar,dto);
                result.add(dto);
            }
        }
        return result;
    }
}
